import time

import allure

from src.api.base_api import BaseApi


class Cp_order(BaseApi):
    def __init__(self, base_url):
        super().__init__(base_url)

    @allure.step('获取合作单列表')
    def list(self, platform='dy', my_type='myCooperationOrder', cooperation_type="", order_sn="", product_id="",
             author_id="", medium_id="", shop_ids="", start_time="", end_time=""):
        '''
        url='https://ck-medium-api-test.cds8.cn/v1/cp_order'
        get
        param:
        cooperation_type: 合作模式 1:纯佣  2:等比例服务费   3:一口价服务费
        '''
        params = {
            'platform': platform,
            'cooperation_type': cooperation_type,
            'product_id': product_id,
            'order_sn': order_sn,
            'author_id': author_id,
            'medium_id': medium_id,
            'shop_ids': shop_ids,
            'start_time': start_time,
            'end_time': end_time,
            'type': my_type,
            'page': 1,
            'size': 20
        }
        res_json = self.send(method='get', path='cp_order', params=params)
        return res_json

    @allure.step('删除合作单')
    def delete(self, cooperation_order_sn='CPETTT202405151559407L7GTKEZ7'):
        '''
        url = 'https://ck-medium-api-test.cds8.cn/v1/cp_order/delete'
        put
        '''

        json_data = {
            "cooperation_order_sn": cooperation_order_sn
        }
        res_json = self.send(method='put', path='cp_order/delete', json=json_data)

        return res_json

    @allure.step('创建合作单')
    def create(self, res_author, sample_res, good_res, service_fee=200, apply_reason='', platform='dy',
               ad_amount=0, is_ad=2, deliver_end_time=0,
               deliver_start_time=time.time(), deliver_type=1,
               cooperation_type=1):
        '''
        创建合作单
        https://ck-medium-api-test.cds8.cn/v1/cp_order
        POST
        参数：
        非必须：
        author_id:达人id
        service_fee：服务费
        deliver_type:交付方式1.直播转成，2.直播拼场，3.视频，如有多个则用，隔开
        cooperation_type：number 合作类型：1.纯佣2.等比例服务费3.一口价服务费
        deliver_start_time：交付开始时间错
        deliver_end_time：交付结束什锦锉
        expect_sales:预期销售额
        is_ad:是否投放广告
        ad_amount:广告费
        contact：联系人
        contact_phone：联系电话
        remark：备注
        check_reason:审核理由
        images:图片
        -
        必须：
        samples:
        - product_id:商品id
        - product_title:商品标题
        - author_cos_ratio:达人佣金率
        - head_cos_ratio:团长佣金率
        - author_price:达人带货价格
        '''
        author_id = res_author.get('author_id')
        author_name = res_author.get('nick_name')

        contact = sample_res.get('receiver')
        contact_phone = sample_res.get('receiver_phone')
        remark = sample_res.get('remark')

        product_id = good_res.get('product_id')
        product_title = good_res.get('title')
        author_cos_ratio = good_res.get('cos_ratio')
        head_cos_ratio = good_res.get('cos_ratio')
        author_price = good_res.get('author_price')

        json_data = {
            "author_id": author_id,
            "platform": platform,
            "author_name": author_name,
            "deliver_start_time": deliver_start_time,
            "deliver_end_time": deliver_end_time,
            "samples": [
                {
                    "product_id": product_id,
                    "product_title": product_title,
                    "author_cos_ratio": author_cos_ratio,
                    "head_cos_ratio": head_cos_ratio,
                    "author_price": author_price,
                    "activity_id": "",
                    "product_type": "",
                    "activity_cos_ratio": 0,
                    "activity_name": ""
                }
            ],
            "apply_reason": apply_reason,
            "images": [],
            "contact": contact,
            "contact_phone": contact_phone,
            "remark": remark,
            "cooperation_type": cooperation_type,
            "service_fee": service_fee,
            "is_ad": is_ad,
            'ad_amount': ad_amount
        }
        url = 'https://ck-medium-api-test.cds8.cn/v1/cp_order'
        # res = requests.post(url=url, json=json_data, headers=self.headers)
        json_res = self.send(method='post', path='cp_order', json=json_data)
        return json_res

    @allure.step('编辑合作单')
    def update(self, res_author, sample_res, good_res, service_fee=200, apply_reason='', platform='dy', order_sn="",
               ad_amount=0, is_ad=2, deliver_end_time=0,
               deliver_start_time=time.time(), deliver_type=1,
               cooperation_type=1):
        '''
        编辑合作单
        https://ck-medium-api-test.cds8.cn/v1/cp_order
        PUT
        参数：
        非必须：
        author_id:达人id
        service_fee：服务费
        deliver_type:交付方式1.直播转成，2.直播拼场，3.视频，如有多个则用，隔开
        cooperation_type：number 合作类型：1.纯佣2.等比例服务费3.一口价服务费
        deliver_start_time：交付开始时间错
        deliver_end_time：交付结束什锦锉
        expect_sales:预期销售额
        is_ad:是否投放广告
        ad_amount:广告费
        contact：联系人
        contact_phone：联系电话
        remark：备注
        check_reason:审核理由
        images:图片
        -
        必须：
        samples:
        - product_id:商品id
        - product_title:商品标题
        - author_cos_ratio:达人佣金率
        - head_cos_ratio:团长佣金率
        - author_price:达人带货价格
        '''
        author_id = res_author.get('author_id')
        author_name = res_author.get('nick_name')

        contact = sample_res.get('receiver')
        contact_phone = sample_res.get('receiver_phone')
        remark = sample_res.get('remark')

        product_id = good_res.get('product_id')
        product_title = good_res.get('title')
        author_cos_ratio = good_res.get('cos_ratio')
        head_cos_ratio = good_res.get('cos_ratio')
        author_price = good_res.get('author_price')

        json_data = {
            "author_id": author_id,
            "platform": platform,
            "author_name": author_name,
            "deliver_start_time": deliver_start_time,
            "deliver_end_time": deliver_end_time,
            "order_sn": order_sn,
            "samples": [
                {
                    "product_id": product_id,
                    "product_title": product_title,
                    "author_cos_ratio": author_cos_ratio,
                    "head_cos_ratio": head_cos_ratio,
                    "author_price": author_price,
                    "activity_id": "",
                    "product_type": "",
                    "activity_cos_ratio": 0,
                    "activity_name": ""
                }
            ],
            "apply_reason": apply_reason,
            "images": [],
            "contact": contact,
            "contact_phone": contact_phone,
            "remark": remark,
            "cooperation_type": cooperation_type,
            "service_fee": service_fee,
            "is_ad": is_ad,
            'ad_amount': ad_amount
        }
        json_res = self.send(method='PUT', path='cp_order', json=json_data)
        return json_res

    @allure.step('修改合作单状态')
    def update_status(self, cooperation_order_sn='CPETTT202405151559407L7GTKEZ7', status=2):
        '''
        url = 'https://ck-medium-api-test.cds8.cn/v1/cp_order/update_status'
        put
        status: 1：验收通过  2:取消   6：验收失败
        '''

        json_data = {
            "cooperation_order_sn": cooperation_order_sn,
            "status": status
        }
        json_res = self.send(method='put', path='cp_order/update_status', json=json_data)

        return json_res
